<template>
  <div class="list" ref="wrapper">
    <div class="content">
    <div class="area">
      <div class="title border-topbottom">当前位置</div>
       <div class="button_list">
       <div class="button_wrap">
          <div class="btn">
           {{currentCity}}
          </div>
        </div>
       </div>
    </div>
     <div class="area">
      <div class="title border-topbottom">热门城市</div>
      <div class="button_list">
        <div class="button_wrap" v-for="(item,index) in hot" :key="index" @click="handleChoseCity(item.name)">
          <div class="btn">
            {{item.name}}
          </div>
        </div>
        </div>
    </div>
     <div class="area" v-for="(item, index) in cities" :key="index" :ref="index"> 
      <div class="title border-topbottom">{{index}}</div>
      <div class="item_list" v-for="(city,index) in item" :key="index" @click="handleChoseCity(city.name)">
        <div class="item_city border-bottom">{{city.name}}</div>
      </div>
    </div>
  
    </div>

  
  </div>
</template>

<script>
import Bscroll from "better-scroll";
import { mapMutations, mapState } from "vuex";
export default {
  name: "list",
  props: {
    hot: Array,
    cities: Object,
    letter: String
  },
  data() {
    return {};
  },
  computed: {
    ...mapState({
      currentCity: "city"
    })
  },
  mounted() {
    // 通过ref获取dom
    this.scroll = new Bscroll(this.$refs.wrapper);
  },
  methods: {
    handleChoseCity(city) {
      this.changeCity(city);
      this.$router.push("/");
    },
    ...mapMutations(["changeCity"])
  },
  watch: {
    // 监听接收到的letter的值的变化，一旦有变化就执行函数体里滚动的方法
    letter() {
      if (this.letter) {
        // console.log(this.$refs);数组A：Array[1]
        const element = this.$refs[this.letter][0];
        console.log(element);
        this.scroll.scrollToElement(element);
      }
    }
  }
};
</script>

<style lang="stylus" scoped>
$bgColor = #00bcd4;

@import '~styles/varibles.styl';

.border-topbottom {
  &:after {
    border-color: #ccc;
  }

  &:before {
    border-color: #ccc;
  }
}

.border-bottom {
  &:before {
    border-color: #ccc;
  }
}

.list {
  width: 100%;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 1.86rem;
  overflow: hidden;
}

.button_wrap {
  float: left;
  width: 33.3333%;
  text-align: center;
  line-height: 0.4rem;
  border-radius: 4px;

  .btn {
    padding: 0 0.1rem;
    box-sizing: border-box;
    border: 1px solid $bgColor;
    margin: 0.1rem;
    border-radius: 4px;
    font-size: 0.24rem;
  }
}

.button_list {
  overflow: hidden;
  padding: 0.1rem 0.6rem 0.1rem 0.1rem;
}

.title {
  line-height: 0.6rem;
  background: #eee;
  padding: 0 0.2rem;
  box-sizing: border-box;
}

.item_city {
  line-height: 0.54rem;
  padding: 0.1rem 0.2rem;
}
</style>